package stack;

import java.util.Arrays;
import java.util.EmptyStackException;

public class MyStack implements IStack{

    private int[] elem;
    private int usedSize;

    private static final int DEFAULT_CAPACITY = 10;

    public MyStack() {
        elem = new int[DEFAULT_CAPACITY];
    }

    @Override
    public void push(int x) {
        if(full()){
            elem= Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize]=x;
        usedSize++;
    }

    @Override
    public int pop() {
        if(empty()){
            throw new EmptyException("栈空了！！");
        }
        int old=elem[usedSize-1];
        usedSize--;
        return old;
    }

    @Override
    public int peek() {
        if(empty()){
            throw new EmptyException("栈空了！！");
        }
        int old=elem[usedSize-1];
        return old;
    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public boolean empty() {
        if(usedSize==0){
            return true;
        }
        return false;
    }

    @Override
    public boolean full() {
        if(usedSize == elem.length){
            return false;
        }
        return false;
    }
}
